/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is KazeOS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2010
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package evisio.states.ask 
{
    import evisio.display.DisplayList;
    import evisio.states.core.CoreState;

    import lunas.components.textinputs.VisualTextInput;

    import system.Strings;
    import system.eden;
    import system.numeric.Range;

    import vegas.models.CoreModelObject;
    import vegas.net.NetServerConnection;

    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.net.Responder;

    /**
     * The ask state class.
     */
    public class AskState extends CoreState 
    {
        /**
         * Creates a new AskState instance.
         */
        public function AskState()
        {
            super();
        }
        
        /**
         * The connection to send the invitation.
         */
        public var connection:NetServerConnection ;
        
        /**
         * The user model of the application.
         */
        public var model:CoreModelObject ;
        
        /**
         * The name text input reference.
         */
        public var nameInput:VisualTextInput ;
        
        /**
         * The name String length range.
         */
        public var nameRange:Range = new Range(3, 15) ;
        
        /**
         * The stratus connection reference.
         */
        public var stratus:NetServerConnection ;
        
        /**
         * Close the state.
         */        
        public override function close():void
        {
            reset() ;
            if ( factory != null )
            {
                var bt:DisplayObject = factory.getObject( DisplayList.DISCONNECT_BUTTON ) as DisplayObject ;
                if ( bt != null )
                {
                    bt.visible = false ;
                }
            }
        }
        
        /**
         * Open the state.
         */
        public override function open():void
        {
            if ( factory != null )
            {
                var bt:DisplayObject = factory.getObject( DisplayList.DISCONNECT_BUTTON ) as DisplayObject ;
                if ( bt != null )
                {
                    bt.visible = true  ;
                }
            }
        }        
        
        /**
         * Reset the form.
         */
        public override function reset( e:Event = null ):void
        {
            super.reset( e ) ;
            if ( nameInput != null )
            {
                nameInput.text = "" ;        
            }
        }
        
        /**
         * Send the invitation.
         */
        public function send( e:Event = null ):void
        {
            
            var to:String   ;
            
            if ( nameInput != null )
            {
                to = nameInput.text ;
            }            
            
            logger.debug(this + " send invitation to : " + to ) ;
            
            var peerID:String ;
            
            if ( stratus != null )
            {
                peerID = stratus.nearID ; // TODO test !!!!!!!!!!!!! nearNonce or nearID
                logger.fatal(this + " farID     : " + stratus.farID) ;
                logger.fatal(this + " farNonce  : " + stratus.farNonce ) ;
                logger.fatal(this + " nearID    : " + stratus.nearID) ;
                logger.fatal(this + " nearNonce : " + stratus.nearNonce) ;
            }
            
            if ( connection != null && connection.connected && stratus != null && stratus.connected && peerID != null )
            {
               if ( to != null && nameRange != null && ! nameRange.isOutOfRange( to.length ) ) 
               {
                    message = Strings.format( getMessage("send_in_progress") , to ) ; 
                    var responder:Responder = new Responder( result, status ) ;
                    connection.call( "sendInvitation", responder, { peerID : peerID , to:to } ) ;
               }
               else
               {
                    message = Strings.format( getMessage("pseudo_invalid") , nameRange.min , nameRange.max )  ; 
                    logger.warn(this + " connect failed, the specified name String must 3 or more letters.") ;
               }
            }
            else
            {
                message = getMessage("application_bug") ; 
                logger.warn(this + " connect failed, the connection reference is null.") ;
            }
        }
        
        /**
         * @private
         */
        protected function result( o:* ):void
        {
            var exist:Boolean = o is Boolean ? o as Boolean : false ;
            if ( exist )
            {
                logger.info( this + " the invitation is sending." ) ;
                message = "" ;  
                // send profile to the guest here
            }
            else
            {
               logger.warn(this + " the guest don't exist in the server, result: " + o ) ;    
               message = getMessage("guest_no_register") ; 
            }
        }
        
        /**
         * @private
         */
        protected function status( info:* ):void
        {
            logger.warn(this + " status : " + eden.serialize(info)) ;
            message = getMessage("send_failed"); 
        }
    }
}
